package com.fubowen.lepton.db.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.fubowen.lepton.db.bean.VisitedBean
import kotlinx.coroutines.flow.Flow

@Dao
interface VisitedDao {

    @Query("select * from VisitedBean")
    fun all(): Flow<List<VisitedBean>>

    @Query("select * from VisitedBean where Id = :id")
    fun get(id: Int): VisitedBean

    @Query("SELECT * FROM VisitedBean ORDER BY timestamp LIMIT 1")
    fun minOfCreateTime(): VisitedBean?

    /**
     * @return 插入数据的主键
     */
    @Insert
    fun set(bean: VisitedBean): Long

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun setOrReplace(bean: VisitedBean): Long

    /**
     * @return 插入数据的主键集合
     */
    @Insert
    fun setList(vararg bean: VisitedBean): LongArray

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun setOrIgnore(list: List<VisitedBean>): LongArray

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun setOrReplace(list: List<VisitedBean>): LongArray

    /**
     * @return 成功更新的数据行数
     */
    @Update
    fun put(bean: VisitedBean): Int

    /**
     * @return 成功删除的数据行数
     */
    @Delete
    fun del(bean: VisitedBean): Int

    @Query("DELETE FROM VisitedBean where timestamp < :minTimestamp")
    fun del(minTimestamp: Long)

    @Query("delete from VisitedBean where id in (:ids)")
    fun del(ids: List<Int>): Int

    @Query("DELETE FROM VisitedBean")
    fun clear()


}